﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Beetle.Sl.EditorControl.Core;

namespace Beetle.Sl.EditorControl.Controls
{
    public class FontFamilySelectPanel : Control
    {
        private ICommand _selectFontFamilyNameCommand = null;
        public event EventHandler SelectedValue;

        public static readonly DependencyProperty FontFamilyNamesProperty = DependencyProperty.Register("FontFamilyNames",
           typeof(System.Collections.ObjectModel.ObservableCollection<FontFamilyInfo>),
           typeof(FontFamilySelectPanel),
           new PropertyMetadata(null, null));

        public System.Collections.ObjectModel.ObservableCollection<FontFamilyInfo> FontFamilyNames
        {
            get
            {
                return (System.Collections.ObjectModel.ObservableCollection<FontFamilyInfo>)this.GetValue(FontFamilySelectPanel.FontFamilyNamesProperty);
            }

            set
            {
                this.SetValue(FontFamilySelectPanel.FontFamilyNamesProperty, value);
            }
        }

        public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value",
          typeof(FontFamilyInfo),
          typeof(FontFamilySelectPanel),
          new PropertyMetadata(null, null));

        public FontFamilyInfo Value
        {
            get
            {
                return (FontFamilyInfo)this.GetValue(FontFamilySelectPanel.ValueProperty);
            }

            set
            {
                this.SetValue(FontFamilySelectPanel.ValueProperty, value);
            }
        }

        public ICommand SelectFontFamilyNameCommand
        {
            get
            {
                if (this._selectFontFamilyNameCommand == null)
                {
                    this._selectFontFamilyNameCommand = new DelegateCommand(this.OnSelectFontFamilyNameCommand);
                }

                return this._selectFontFamilyNameCommand;
            }
        }

        public FontFamilySelectPanel()
        {
            this.DefaultStyleKey = typeof(FontFamilySelectPanel);
        }

        private void OnSelectFontFamilyNameCommand(object arg)
        {
            FontFamilyInfo fontFamilyInfo = arg as FontFamilyInfo;
            this.Value = fontFamilyInfo;

            this.RaiseSelectedValue();
        }

        private void RaiseSelectedValue()
        {
            if (this.SelectedValue != null)
            {
                this.SelectedValue(this, null);
            }
        }
    }
}
